package com.example.business.service;

import cn.hutool.core.bean.BeanUtil;
import com.example.common.response.PageResponse;
import com.example.business.entity.SbOrderItem;
import com.example.business.entity.SbOrderItemExample;
import com.example.business.mapper.SbOrderItemMapper;
import com.example.business.request.SbOrderItemListRequest;
import com.example.business.response.SbOrderItemResponse;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.example.common.util.SnowflakeUtil;
import com.example.business.request.SbOrderItemRequest;

@Service
public class SbOrderItemService {
    private static final Logger log = LoggerFactory.getLogger(SbOrderItemService.class);
    @Autowired
    SbOrderItemMapper sbOrderItemMapper;

    public PageResponse<SbOrderItemResponse> list(SbOrderItemListRequest request) {
        SbOrderItemExample sbOrderItemExample = new SbOrderItemExample();
        // 按 id 字段逆序排序
        sbOrderItemExample.setOrderByClause("id desc");

        // 查询条件
        SbOrderItemExample.Criteria criteria = sbOrderItemExample.createCriteria();

        // 增加查询条件：关键词搜索
        if (request.getKeyword() != null && !request.getKeyword().isEmpty()) {
            String keyword = "%" + request.getKeyword() + "%";
        }

        PageHelper.startPage(request.getPage(), request.getSize());
        log.info("当前页码 current_page = {}", request.getPage());
        log.info("每页记录数 per_page = {}", request.getSize());

        List<SbOrderItem> sbOrderItems = sbOrderItemMapper.selectByExample(sbOrderItemExample);

        PageInfo<SbOrderItem> pageInfo = new PageInfo<>(sbOrderItems);
        log.info("总记录数 total = {}", pageInfo.getTotal());
        log.info("总页数 pages = {}", pageInfo.getPages());

        List<SbOrderItemResponse> list = BeanUtil.copyToList(sbOrderItems, SbOrderItemResponse.class);

        PageResponse<SbOrderItemResponse> pageResponse = new PageResponse<>();
        pageResponse.setTotal(pageInfo.getTotal());
        pageResponse.setList(list);

        return pageResponse;
    }
    public void save(SbOrderItemRequest request) {
        SbOrderItem sbOrderItem = BeanUtil.copyProperties(request, SbOrderItem.class);
        DateTime now = DateTime.now();
        if (ObjectUtil.isNull(sbOrderItem.getId()) || sbOrderItem.getId() == 0L) {
            sbOrderItem.setId(SnowflakeUtil.getId());
            sbOrderItem.setCreatedAt(now);
            sbOrderItem.setUpdatedAt(now);
            sbOrderItemMapper.insert(sbOrderItem);
        } else {
            sbOrderItem.setUpdatedAt(now);
            sbOrderItemMapper.updateByPrimaryKeySelective(sbOrderItem);
        }
    }

    public int deleteBatch(List<Long> ids) {
        SbOrderItemExample sbOrderItemExample = new SbOrderItemExample();
        SbOrderItemExample.Criteria criteria = sbOrderItemExample.createCriteria();
        criteria.andIdIn(ids);
        return sbOrderItemMapper.deleteByExample(sbOrderItemExample);
    }
}
